package cn.cau.epidemicsurveysystem.control;

import cn.cau.epidemicsurveysystem.bean.*;
import cn.cau.epidemicsurveysystem.dao.AreaRepository;
import cn.cau.epidemicsurveysystem.service.DiseaseInfoService;
import cn.cau.epidemicsurveysystem.service.EpidMissionRecordService;
import cn.cau.epidemicsurveysystem.service.PatientInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @projectName: gp2022
 * @package: cn.cau.epidemicsurveysystem.control
 * @className: ReportController
 * @author: zfy
 * @description: 医院上报有关页面跳转及数据处理
 * @date: 2022/3/3 0003 15:22
 * @version: 1.0
 */

@Controller
public class ReportController {

    @Autowired
    PatientInfoService patientInfoService;
    @Autowired
    EpidMissionRecordService epidMissionRecordService;
    @Autowired
    DiseaseInfoService diseaseInfoService;
    @Autowired
    AreaRepository areaRepository;

    /**
     * @param :binder
     * @return
     * @author zfy
     * @description 将前端Date类型修改为java.sql.Date类型
     * @date 2022/3/1 16:45
     */
    @InitBinder
    public void initBinder(ServletRequestDataBinder binder){
        //只要网页中传来的数据格式为yyyy-MM-dd 就会转化为Date类型
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));
    }

    /**
     * @param :requestTime，response，request，session
     * @return ModelAndView
     * @author zfy
     * @description 提交上报
     * @date 2022/2/27 16:45
     */
    @RequestMapping(value = "/reportSubmit", method = RequestMethod.POST)
    public ModelAndView reportSubmit(@RequestParam("time1") Date requestTime, HttpServletResponse response, HttpServletRequest request, HttpSession session)throws IOException {
        ModelAndView modelAndView=new ModelAndView();
//        System.out.println(request.getParameter("patientName"));



//        新增病人
//        查找是否有记录
//        System.out.println(request.getParameter("patientIdCard"));
        PatientInfoBean patientInfoBean=patientInfoService.findPatientInfoBeanByPatientIdCard(request.getParameter("patientIdCard"));
        if(patientInfoBean==null){
            patientInfoBean=patientInfoService.save(new PatientInfoBean());
            patientInfoBean.setPatientIdCard(request.getParameter("patientIdCard"));
        }
        patientInfoBean.setPatientTele(request.getParameter("patientTele"));
        patientInfoBean.setPatientAddress(request.getParameter("patientAddress"));
        patientInfoBean.setPatientName(request.getParameter("patientName"));
        patientInfoService.save(patientInfoBean);


//        新增疾病
        DiseaseInfoBean diseaseInfoBean=diseaseInfoService.findDiseaseInfoBeanByDiseaseName(request.getParameter("diseaseName"));
        if(diseaseInfoBean==null){
            diseaseInfoBean=diseaseInfoService.save(new DiseaseInfoBean());
            diseaseInfoBean.setDiseaseName(request.getParameter("diseaseName"));
            diseaseInfoService.save(diseaseInfoBean);
        }

//        新增任务
        EpidMissionRecordBean epidMissionRecordBean=epidMissionRecordService.save(new EpidMissionRecordBean());
        StaffInfoBean staffInfoBean=(StaffInfoBean)session.getAttribute("staffInfo");
        epidMissionRecordBean.setMissionName(diseaseInfoBean.getDiseaseName()+"流调任务");
        System.out.println(staffInfoBean.getStaffName());
        epidMissionRecordBean.setPetitionerId(staffInfoBean.getStaffId());
        epidMissionRecordBean.setPetitionerName(staffInfoBean.getStaffName());
        epidMissionRecordBean.setPatientId(patientInfoBean.getPatientId());
        epidMissionRecordBean.setPatientName(patientInfoBean.getPatientName());
        epidMissionRecordBean.setPatientTele(request.getParameter("patientTele"));
        epidMissionRecordBean.setAssignPlace(request.getParameter("area"));
        epidMissionRecordBean.setRequestDetails(request.getParameter("details"));
        epidMissionRecordBean.setRequestTime(requestTime);
        epidMissionRecordBean.setMissionState(0);
        epidMissionRecordBean.setRequestHospital(staffInfoBean.getStaffWorkplace());
        epidMissionRecordBean.setSurveyLacation(patientInfoBean.getPatientAddress());
        epidMissionRecordBean.setDiseaseId(diseaseInfoBean.getDiseaseId());
        epidMissionRecordBean.setDiseaseName(diseaseInfoBean.getDiseaseName());
        epidMissionRecordService.save(epidMissionRecordBean);



        List<EpidMissionRecordBean> epidMissionRecordBeans = epidMissionRecordService.findEpidMissionRecordBeansByRequestHospitalOrderByRequestTime(staffInfoBean.getStaffWorkplace());
        modelAndView.addObject("epidMissionRecordBeans",epidMissionRecordBeans);

        modelAndView.setViewName("epid_reportrecord");
        response.setContentType("text/html; charset=utf-8");
        PrintWriter out=response.getWriter();
        out.println("<script language='javascript'>");
        out.println("alert('流调请求已上报');");
        out.println("</script>");
        return modelAndView;
    }
    /**
     * @param :session
     * @return ModelAndView
     * @author zfy
     * @description 历史上报
     * @date 2022/3/3 9:10
     */
    @RequestMapping(value = "/reportList")
    public ModelAndView reportList(HttpSession session){
        ModelAndView modelAndView=new ModelAndView();
        StaffInfoBean staffInfoBean=(StaffInfoBean)session.getAttribute("staffInfo");
        List<EpidMissionRecordBean> epidMissionRecordBeans = epidMissionRecordService.findEpidMissionRecordBeansByRequestHospitalOrderByRequestTime(staffInfoBean.getStaffWorkplace());

        modelAndView.addObject("epidMissionRecordBeans",epidMissionRecordBeans);
        modelAndView.setViewName("epid_reportrecord");
        return modelAndView;
    }

    /**
     * @param :missionId
     * @return ModelAndView
     * @author zfy
     * @description 历史上报详情查看
     * @date 2022/3/3 10:10
     */
    @RequestMapping(value = "/reportDetails")
    public ModelAndView reportDetails(@RequestParam("missionId")int missionId){
        ModelAndView modelAndView=new ModelAndView();
        EpidMissionRecordBean epidMissionRecordBean=epidMissionRecordService.findEpidMissionRecordBeanByMissionId(missionId);
        modelAndView.addObject("epidMissionRecordBean",epidMissionRecordBean);
        modelAndView.setViewName("epid_reportdetail");
        return modelAndView;
    }

    /**
     * @param :missionId, session, response
     * @return ModelAndView
     * @author zfy
     * @description 历史上报取消
     * @date 2022/3/3 11:10
     */
    @RequestMapping(value = "/reportCancel")
    public ModelAndView reportCancel(@RequestParam("missionId")int missionId,HttpSession session,HttpServletResponse response)throws IOException{
        ModelAndView modelAndView=new ModelAndView();
        EpidMissionRecordBean epidMissionRecordBean=epidMissionRecordService.findEpidMissionRecordBeanByMissionId(missionId);
        epidMissionRecordBean.setMissionState(3);
        epidMissionRecordService.save(epidMissionRecordBean);
        StaffInfoBean staffInfoBean=(StaffInfoBean)session.getAttribute("staffInfo");
        List<EpidMissionRecordBean> epidMissionRecordBeans = epidMissionRecordService.findEpidMissionRecordBeansByRequestHospitalOrderByRequestTime(staffInfoBean.getStaffWorkplace());

        modelAndView.addObject("epidMissionRecordBeans",epidMissionRecordBeans);
        modelAndView.setViewName("epid_reportrecord");

        response.setContentType("text/html; charset=utf-8");
        PrintWriter out=response.getWriter();
        out.println("<script language='javascript'>");
        out.println("alert('流调请求已取消');");
        out.println("</script>");

        return modelAndView;
    }

    /**
     * @param :
     * @return ModelAndView
     * @author zfy
     * @description 新增上报
     * @date 2022/3/3 15:43
     */
    @RequestMapping(value = "/reportManage")
    public ModelAndView reportManage(){
        ModelAndView modelAndView=new ModelAndView();
        List<AreaBean> areaBeans=areaRepository.findAll();
        modelAndView.addObject("areaBeans",areaBeans);
        modelAndView.setViewName("epid_reportmanage");
        return modelAndView;
    }
}
